package com.ruoyi.web.controller.system;

import java.rmi.ServerException;
import java.security.PrivateKey;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.config.properties.RsaKeyProperties;
import com.ruoyi.framework.util.RsaUtils;
import com.ruoyi.system.domain.vo.RouterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.service.ISysMenuService;

import javax.annotation.Resource;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
@Api(tags = {"登录模块API"})
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private RsaKeyProperties rsaKeyProperties;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    @ApiOperation(value = "账号登录")
    public AjaxResult<Map<String, Object>> login(@RequestBody LoginBody loginBody) throws Exception {
        try {
            // AjaxResult ajax = AjaxResult.success();
            Map<String, Object> map = new HashMap<>();
            PrivateKey privateKey = rsaKeyProperties.getPrivateKey();
            String username = loginBody.getUsername();
            if(StringUtils.isBlank(username)){
                throw new ServerException("用户名不能为空");
            }
            username = RsaUtils.decrypt(username, privateKey);
            String password = loginBody.getPassword();
            if(StringUtils.isBlank(password)){
                throw new ServerException("密码不能为空");
            }
            password = RsaUtils.decrypt(password, privateKey);
            // 生成令牌
            String token = loginService.login(username, password, loginBody.getCode(),
                    loginBody.getUuid());
            map.put(Constants.TOKEN, token);
            return AjaxResult.success(map);
        } catch (Exception e) {
            e.fillInStackTrace();
            return AjaxResult.error("账号或密码错误");
        }

    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    @ApiOperation(value = "获取用户信息")
    public AjaxResult<Map<String, Object>> getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        // AjaxResult ajax = AjaxResult.success();
        Map<String, Object> map = new HashMap<>();
        map.put("user", user);
        map.put("roles", roles);
        map.put("permissions", permissions);
        return AjaxResult.success(map);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    @ApiOperation(value = "路由信息")
    public AjaxResult<List<SysMenu>> getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        if (CollectionUtils.isEmpty(menus)){
            return AjaxResult.error("账号未分配角色,请联系管理员");
        }
        return AjaxResult.success(menus);
    }
}
